/**
 * 搜索入口
 * searchEntry.js
 * -----------------------------------
 * 18/03/26 Jerry 新增
 */

var
helper = require('../../../utils/helper.js');

Page({

/* ------------------------------
 页面数据
------------------------------ */
data: {
  inputable: true,
  focus: true,
  hiddenClear: 'hidden',
  hiddenHistory: 'hidden'
},
/* ------------------------------
 页面加载
------------------------------ */
onLoad: function(opts){

  helper.navTitle('搜索');

  var
  keyword = opts.keyword,
  hiddenClear = keyword ? '' : 'hidden',
  // 如果没有传入关键词，显示搜索历史
  histories = !keyword ? wx.getStorageSync('searchHistory') : null,
  hiddenHistory = !keyword ? '' : 'hidden';

  // 绑定数据
  helper.setData(this, {
    inputKeyword: keyword,
    keyword: keyword,
    hiddenClear: hiddenClear,
    hiddenHistory: hiddenHistory
  });

  // 设置scroll-view高度
  helper.setScrollViewHeight(this);

  // 尝试按关键词搜索
  this.querySKUs();

},
/* ------------------------------
 页面触底
------------------------------ */
scrollToLower: function(){

  // 分页查询（标识scrollend）
  this.querySKUs('scrollend');
},
/* ------------------------------
 处理搜索
------------------------------ */
searchConfirm: function(e){
  // 搜索SKU
  this.querySKUs();
},
/* ------------------------------
 按关键词查询SKU列表
------------------------------ */
querySKUs: function(paging){


  var
  that = this,
  existSKUs = this.data.skus,
  pageIndex = helper.nextPageIndex( existSKUs, paging ),
  keyword = this.data.keyword;

  if (pageIndex < 0 || !keyword)
    return;

  // 更新本地搜索历史
  this.refreshHistories(keyword);

  // 如果分页为0，清空已绑定的SKU（保证 scroll-view 滚动到顶部）
  if (!pageIndex)
    this.setData({ skus: null, hiddenHistory: 'hidden' });

  helper.request({
    url: [ 'wx/sku/list?pageIndex=', pageIndex || 0, '&s=', encodeURIComponent(keyword) ].join(''),
    success: this.bindSKUs
  });

},
/* ------------------------------
 绑定显示SKU列表
------------------------------ */
bindSKUs: function(ret){

  // 绑定SKU图片完整URL
  helper.bindFullImgUrl(ret.records);

  // 绑定数据，同时隐藏搜索历史UI
  helper.setData(this, { skus: helper.concatPaging(this, 'skus', ret), hiddenHistory: 'hidden' }, false);

},
/* ------------------------------
 确认是否显示"清除关键词"图标
------------------------------ */
searchInput: function(e){
  var keyword = e.detail.value;
  this.setData({ keyword: keyword, hiddenClear: keyword ? '' : 'hidden' });
},
/* ------------------------------
 输入框聚焦时，绑定显示搜索历史
------------------------------ */
searchFocus: function(){

  var
  keyword = this.data.keyword,
  histories;

  if (keyword)
    return;

  this.setData({
    histories: wx.getStorageSync('searchHistory'),
    hiddenHistory: ''
  });

},
/* ------------------------------
 清除输入的关键词
------------------------------ */
searchClear: function(){

  this.setData({
    inputKeyword: null,
    keyword: null,
    hiddenClear: 'hidden',
    hiddenHistory: ''
  });

},
/* ------------------------------
 返回首页
------------------------------ */
searchCancel: function(){
  helper.switchTab('index');
},
/* ------------------------------
 处理历史关键词Click
------------------------------ */
searchHistoryClick: function(e){

  var keyword = e.target.dataset.keyword;

  // 刷新UI（同时更新 inputKeyword，把内容显示在输入框）
  this.setData({
    inputKeyword: keyword,
    keyword: keyword,
    hiddenClear: ''
  });

  // 搜索SKU
  this.querySKUs();

},
/* ------------------------------
 刷新本地搜索历史
------------------------------ */
refreshHistories: function(keyword){

  if (!keyword)
    return;

  var
  histories = wx.getStorageSync('searchHistory'),
  idx;

  if (!histories)
    histories = [];

  for (idx = 0; idx < histories.length; idx++){

    if (histories[idx] != keyword)
      continue;

    histories.splice(idx, 1);
    break;

  }

  histories.unshift(keyword);

  // 将更新的数据保存到本地存储
  wx.setStorageSync('searchHistory', histories);

},
/* ------------------------------
 清空本地搜索历史
------------------------------ */
searchHistoryClear: function(){

  wx.removeStorageSync('searchHistory');
  this.setData({ histories: null, hiddenHistory: '' });

}

})
